import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

import javax.rmi.PortableRemoteObject;

public class Warehouse extends PortableRemoteObject implements
		WarehouseInterface {
	private static final long serialVersionUID = -149510973712369413L;
	private static HashMap<Integer, String> sessions;
	private static HashMap<Integer, Properties> orders;
	private static Properties productList;
	private int lastSession = 1;

	public Warehouse() throws RemoteException {
		sessions = new HashMap<Integer, String>();
		orders = new HashMap<Integer, Properties>();
		System.out.println("Creating server object");
	}

	public void setProductList(Properties productList) {
		Warehouse.productList = productList;
	}

	public String getMessage() {
		System.out.println("Returning TEST-message");
		return "TEST";
	}

	public Properties getProducts(String category) {
		if (category.equalsIgnoreCase("")) {
			System.out.println("Returning products");
			return productList;
		} else {
			System.out.println("Returning products: [" + category + "]");
			Properties returning = new Properties();
			Enumeration<Object> e = productList.keys();
			while (e.hasMoreElements()) {
				Product p = (Product) e.nextElement();
				int a = (Integer) productList.get(p);
				if (p.getCategory().equalsIgnoreCase(category)) {
					returning.put(p, a);
				}
			}
			return returning;
		}
	}

	@Override
	public int newSession(String name) {
		if (name.equalsIgnoreCase("")) {
			return 0;
		} else {
			int session = this.lastSession++;
			sessions.put(session, name);
			System.out.println("session created for " + name + ": " + session);
			orders.put(session, new Properties());
			return session;
		}
	}

	@Override
	public boolean order(String name, int sessionId, String productCode,
			int amount) {
		Properties userOrders = orders.get(sessionId);
		if (userOrders.containsKey(productCode)) {
			int current = (Integer) userOrders.get(productCode);
			int newAmount = current + amount;
			userOrders.put(productCode, newAmount);
		} else {
			userOrders.put(productCode, amount);
		}
		orders.put(sessionId, userOrders);
		return true;
	}

	@Override
	public Properties getOrderOverview(String name, int sessionId) {
		Properties orderOverview = new Properties();
		ProductInterface product = null;
		Properties wagentje = orders.get(sessionId);
		for (Object o : wagentje.keySet()) {
			String key = (String) o;
			product = getProduct(key);
			orderOverview.put(product, wagentje.get(key));
		}
		return orderOverview;
	}

	private ProductInterface getProduct(String productCode) {
		ProductInterface p = null;
		for (Object o : productList.keySet()) {
			try {
				p = (ProductInterface) o;
				if (p.getCode().equals(productCode)) {
					return p;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return p;
	}

	@Override
	public boolean confirmOrder(String name, int sessionId) {
		ProductInterface product = null;
		Properties cart = orders.get(sessionId);
		for (Object o : cart.keySet()) {
			String key = (String) o;
			product = getProduct(key);
			int substract = (Integer) cart.get(key);
			int amount = (Integer) productList.get(product);
			if (substract >= amount) {
				return false;
			}
		}
		for (Object o : cart.keySet()) {
			String key = (String) o;
			product = getProduct(key);
			int substract = (Integer) cart.get(key);
			int amount = (Integer) productList.get(product);
			productList.put(product, amount - substract);
		}
		return true;
	}

}
